#include "sys.h"
#include "ds1302.h"
bit flag500ms,flag1000ms,flag200ms;
u16 T1_Count2;
u8 T1_Count1;
int temp;
void main(void)
{
	All_init();
	Timer0Init();
	Timer1Init();
	Ds1302_init();
	getTime();
	while(1)
	{
		if(flag500ms)
		{
			flag500ms = 0;
			if(Start_18b20())
			{
				temp = Read_temp()*0.0625 + 0.5;
			}
			if(timeMode == 0)
			{
				getTime();
			}		
		}
		key_action();
		show();
		Clock_ON();
	}
}

void Clock_ON()
{
	if(TR1)
	{
		if(flag200ms)
		{
			led_illume(0xfe);
		}else{
			led_illume(0xff);
		}
	}
	if((S4 == 0) || (S5 == 0) || (S6 == 0) || (S7 == 0))
	{
		TR1 = 0;
		led_illume(0xff);
		T1_Count1 = 0;
		T1_Count2 = 0;
	}
}

void timer0() interrupt 1
{
	static u16 T0_Count1,T0_Count2;
	T0_Count1++;
	T0_Count2++;
	if(T0_Count1 == 500)
	{
		T0_Count1  = 0;
		flag500ms = 1;
	}
	if(T0_Count2 == 500)
	{
		T0_Count2 = 0;
		flag1000ms = ~flag1000ms;
	}
	if(time.hour == clock.hour && time.min == clock.min && time.sec == clock.sec)
	{
		TR1 = 1;
	}
	Nixie_show();
	Nixie_scan();
}

void timer1() interrupt 3
{
 
	T1_Count1++;
	T1_Count2++;
	if(T1_Count1 == 200)
	{
		T1_Count1 = 0;
		flag200ms = ~flag200ms;
	}
	if(T1_Count2 == 5000)
	{
		TR1 = 0;
		led_illume(0xff);
		T1_Count2 = 0;
		T1_Count1 = 0;
	}
}